package de.koller.occultation.render;

import de.koller.aframe.plain.Sprite;
import de.koller.occultation.ONode;
import de.koller.occultation.OccultationStart;
import de.koller.occultation.engine.tile.Tile;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class TileViewBuffer extends ONode {

	public final TileViewSprite tileViewSprite = new TileViewSprite();
	
	public TileViewBuffer() {
		
	}

	@Override
	protected void updateS() {
		super.updateS();
		
		tileViewSprite.updateView();
	}
	
	public void renderThis( Graphics2D g ) {
		tileViewSprite.render( g, 0, 0, 
				OccultationStart.WIDTH / OccultationStart.TILE_SIZE,
				OccultationStart.HEIGHT / OccultationStart.TILE_SIZE, 0 );
	}
	
	private class TileViewSprite extends Sprite {

		private TileViewSprite() {
			super( OccultationStart.WIDTH, OccultationStart.HEIGHT );
			this.raster.clear( 0xffff0000 );
		}
		
		public void updateView() {
			Tile[][] m = oroot.engine.tileMap.map;
			
			AffineTransform a = graphics.getTransform();
			graphics.scale( OccultationStart.TILE_SIZE, OccultationStart.TILE_SIZE );
			
			this.raster.clear( 0xff000000 );
			for( int x = 0; x < m.length; x++ )
				for( int y = 0; y < m[0].length; y++ )
					renderTile( m[x][y], x, y );
			
			graphics.setTransform( a );
		}
		
		private void renderTile( Tile t, int tx, int ty ) {
			AffineTransform a = graphics.getTransform();
			graphics.translate( tx, ty );
			
			t.visual.render( graphics );
			
			graphics.setTransform( a );
		}
	}
	
}
